<?php
/**
 * @package YARBB
 * @copyright Авторские права (C) 2009 www.yar-it.com. Все права защищены.
 * @license Лицензия http://www.gnu.org/licenses/gpl-2.0.htm GNU/GPL, или help/license.php
 * YARBB! - свободное программное обеспечение распространяемое по условиям лицензии GNU/GPL
 * Для получения информации о используемых расширениях и замечаний об авторском праве, смотрите файл copyright.php.
 */
// no direct access
defined( '_VALID_MOS' ) or die( 'Direct Access to this location is not allowed.' );

// Tell common.php that we don't want output buffering
define('YARBB_DISABLE_BUFFERING', 1);

if (isset($_GET['i_per_page']) && isset($_GET['i_start_at'])) {
	$per_page = intval($_GET['i_per_page']);
	$start_at = intval($_GET['i_start_at']);
	if ($per_page < 1 || $start_at < 1)
		mosRedirect( '', _Y_404 );
	@set_time_limit(0);
	// If this is the first cycle of posts we empty the search index before we proceed
	if (isset($_GET['i_empty_index'])) {
		// This is the only potentially "dangerous" thing we can do here, so we check the referer
		////confirm_referrer('admin_maintenance.php');
		$truncate_sql = ($db_type != 'sqlite') ? 'TRUNCATE TABLE ' : 'DELETE FROM ';
		//s
		$database->setQuery($truncate_sql.'#__forum_search_matches')->query();
		$database->setQuery($truncate_sql.'#__forum_search_words')->query();
		$result = $database->setQuery('ALTER TABLE #__forum_search_words auto_increment=1')->query();

	}
	$end_at = $start_at + $per_page;
	?>

<p><?php echo _Y_MAIINDEXDESC; ?></p>
	<?php
	require $yarbb_admin_path.'/include/search_idx.php';

/////////////////////////////////BEGIN FluxBB UPGRADE/////////////////
//s
	$sql='SELECT id FROM #__forum_topics WHERE id>='.$start_at.' ORDER BY id LIMIT '.$per_page;
	$result = $database->setQuery($sql);// or error('Unable to fetch topic list', __FILE__, __LINE__, $database->stderr());
	$topics = array();
	$rows=$database->loadResultArray();
	foreach ($rows as $cur_topic )
		$topics[] = $cur_topic;

	//s
	$database->setQuery('SELECT topic_id, id, message FROM #__forum_posts WHERE topic_id IN ('.implode(',', $topics).') ORDER BY topic_id');// or error('Unable to fetch topic/post info', __FILE__, __LINE__, $database->stderr());


/////////////////////////////////END FluxBB UPGRADE///////////////////
	$cur_topic = 0;
	//s - ?
	$rows=$database->loadRowList();
	foreach($rows as $cur_post ) {
		if ($cur_post[0] <> $cur_topic) {
			// Fetch subject and ID of first post in topic
			//s?
			$database->setQuery('SELECT p.id, t.subject, MIN(p.posted) AS first FROM #__forum_posts AS p INNER JOIN #__forum_topics AS t ON t.id=p.topic_id WHERE t.id='.$cur_post[0].' GROUP BY p.id, t.subject ORDER BY first LIMIT 1');// or error('Unable to fetch topic info', __FILE__, __LINE__, $database->stderr());
			list($first_post, $subject) = $database->loadRow();
			$cur_topic = $cur_post[0];
		}
		echo 'Processing post <strong>'.$cur_post[1].'</strong> in topic <strong>'.$cur_post[0].'</strong><br />'."\n";
		if ($cur_post[1] == $first_post)	// This is the "topic post" so we have to index the subject as well
			update_search_index('post', $cur_post[1], $cur_post[2], $subject);
		else
			update_search_index('post', $cur_post[1], $cur_post[2]);
	}
	// Check if there is more work to do
	$database->setQuery('SELECT id FROM #__forum_topics WHERE id>'.$end_at);// or error('Unable to fetch topic info', __FILE__, __LINE__, $database->stderr());
	$database->query();
	$query_str = ($database->getNumRows()) ? '?i_per_page='.$per_page.'&i_start_at='.$end_at : '';
//s	$db->end_transaction();
//s	$db->close();

	////////////////////////////////////
	$query = "SELECT id FROM #__menu WHERE link='index.php?option=com_forum'";
	$database->setQuery($query);
	$yarbb_Itemid = $database->loadResult();
	$yarbb_path1 = "index.php?option=com_forum&Itemid=".$yarbb_Itemid;

	exit('<script type="text/javascript">window.location="index2.php?option=com_forum&task=maintenance&'.$query_str.'"</script><br />'._Y_MAINONREDIR.' <a href="index2.php?option=com_forum&amp;task=maintenance'.$query_str.'">'._Y_MAINONREDIR2.'');
}
else {
// Get the first post ID from the db
//s
	$database->setQuery('SELECT id FROM #__forum_topics ORDER BY id LIMIT 1');// or error('Unable to fetch topic info', __FILE__, __LINE__, $database->stderr());
	$database->query();
	if ($database->getNumRows())
		$first_id = $database->loadResult();

	?>
<table class="adminheading">
	<tr>
		<th class="menus"> <span><?php echo _Y_MAIINDEX; ?></span> </th>
	</tr>
</table>
<form method="GET" action="index2.php?option=com_forum&amp;task=maintenance">
	<p><?php echo _Y_MAIINDEX2; ?></p>
	<table class="adminform">
		<tr>
			<th colspan="2"><?php echo _Y_MAIINDEX; ?></th>
		</tr>
		<tr>
			<td width="20%"><?php echo _Y_MAICIKL; ?></td>
			<td><input type="hidden" name="option" value="com_forum" />
				<input type="hidden" name="task" value="maintenance" />
				<input type="text" name="i_per_page" size="7" maxlength="7" value="100" tabindex="1" />
				<span><?php echo mosToolTip(_Y_MAICIKL2); ?></span> </td>
		</tr>
		<tr>
			<td><?php echo _Y_MAIIDSORT; ?></td>
			<td><input type="text" name="i_start_at" size="7" maxlength="7" value="<?php echo (isset($first_id)) ? $first_id : 0 ?>" tabindex="2" />
				<span><?php echo mosToolTip(_Y_MAIIDSORT2); ?></span> </td>
		</tr>
		<tr>
			<td><?php echo _Y_MAINONINDEX; ?></td>
			<td><span>
					<input type="checkbox" name="i_empty_index" value="1" tabindex="3" checked="checked" />
						<?php echo mosToolTip(_Y_MAINONINDEX2); ?></span> </td>
		</tr>
		<tr>
			<td><input type="submit" name="rebuild_index" value="<?php echo _RESTORE; ?>" tabindex="4" /></td>
		</tr>
	</table>
</form>
	<?php
}